// HTTP请求体解析的中间件
const bodyParser = require("body-parser");

function registerRoutes(app) {
  let mockLastIndex;
  // es6 polyfill
  const { default: mocks } = require("./index.js");
  for (const mock of mocks) {
    app[mock.type](mock.url, mock.response);
    mockLastIndex = app._router.stack.length;
  }
  const mockRoutesLength = Object.keys(mocks).length;
  return {
    mockRoutesLength: mockRoutesLength,
    mockStartIndex: mockLastIndex - mockRoutesLength
  };
}

module.exports = app => {
  require("@babel/register");
  app.use(bodyParser.json()); // 解析JSON格式
  app.use(
    bodyParser.urlencoded({
      extended: true
    })
  );
  registerRoutes(app);
};
